package com.muy.mpc.intellij.codeinsight.postfix.utils;

import com.google.common.collect.Lists;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpressionStatement;
import com.intellij.psi.PsiWhiteSpace;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @Author jiyanghuang
 * @Date 2024/7/18 21:26
 */
public class LinePsiElementUtils {

    public static List<String> lineVariable(@NotNull PsiElement element) {
        List<String> re = Lists.newArrayList();
        PsiElement elementP = element.getParent();
        PsiElement prevSib = elementP.getPrevSibling();
        if (prevSib instanceof PsiWhiteSpace) {
            return re;
        }
        while (prevSib instanceof PsiExpressionStatement) {
            re.add(prevSib.getText().substring(0, prevSib.getText().length() - 1));
            prevSib = prevSib.getPrevSibling();
        }
        return Lists.reverse(re);
    }

    public static PsiElement elementForRemoving(PsiElement element) {
        PsiElement elementP = element.getParent();
        PsiElement prevSib = elementP.getPrevSibling();
        if (prevSib instanceof PsiWhiteSpace) {
            return element;
        }
        while (prevSib instanceof PsiExpressionStatement) {
            prevSib = prevSib.getPrevSibling();
        }
        return prevSib.getNextSibling();
    }
}

